#include "TPM_HAL_KA.h"


uint8 u8TPM_CheckTOF(void) 
{
    return TPMSC_TOF;  
}

/*
void vfnTPM_OvInterruptEnable(void)          Interrupts will not be used
{
    TPMSC_TOIE=1;  
}

void vfnTPM_OvInterruptDisable(void) 
{
    TPMSC_TOIE=0; 
} 
*/
/*
void vfnTPM_SelectCPWM(uint8 u8channel, uint8 u8config) 
{
    TPMSC_CPWMS=1;  // CHANNEL OPERATES IN CENTER-ALIGNED PWM MODE  
    
    if (u8channel == 0)
    {
        TPMC0SC_ELS0x = u8config;
    } 
    else if (u8channel == 1)
    {
        TPMC1SC_ELS1x = u8config;
    } 
    else
    {
        return;
    }
}
*/
void vfnTPM_SelectClock (uint8 u8clock)
{
    TPMSC_CLKSx = u8clock;
}


void vfnTPM_SetPrescalerDivisor(uint8 u8prescaler) 
{
    if (u8prescaler < 8)
    {
        TPMSC_PS = u8prescaler;
    }
    else 
    {
        TPMSC_PS = TPM_PRESCALER_1;
    }
}

uint16 u16TPM_ReadCounter(void)
{
    return TPMCNT;
}

void vfnTPM_SetModulo(uint16 u16modulo)
{
    TPMMODH = (uint8)(u16modulo >> 8);
    TPMMODL = (uint8)(u16modulo);
}

void vfnTPM_ChannelInterruptEnable(uint8 u8channel) 
{
    if (u8channel == 0) 
    {
        TPMC0SC_CH0IE=1;
    }
    else if (u8channel == 1)
    {
        TPMC1SC_CH1IE=1;
    }
    else
    {
        return;
    }
}

uint8 u8TPM_CheckChannelFlag(uint8 u8channel)
{
    if (u8channel == 0) 
    {
        return TPMC0SC_CH0F;
    }
    else if (u8channel == 1)
    {
        return TPMC1SC_CH1F;
    }
    else
    {
        return 0;
    }
}

void vfnTPM_ClearChannelFlag(uint8 u8channel)
{
    if (u8channel == 0) 
    {
        TPMC0SC_CH0F=0;
    }
    else if (u8channel == 1) 
    {
        TPMC1SC_CH1F=0; 
    }
    else
    {
        return;
    }
}

void vfnTPM_ChannelEdgeAligned(uint8 u8channel, uint8 u8config)
{
    if(u8channel==0)
    {
        TPMSC_CPWMS=0;
        TPMC0SC_MS0B=1; 
        TPMC0SC_ELS0x=u8config;  
       
    }
    else if(u8channel==1)
    {
        TPMSC_CPWMS=0;
        TPMC1SC_MS1B=1;
        TPMC1SC_ELS1x=u8config;
    }
    else
    {
        return;
    }
}
/*
void vfnTPM_ChannelInputCapture(uint8 u8channel,uint8 u8config)
{
    if(u8channel==0)
    {
        TPMSC_CPWMS=0;
        TPMC0SC_MS0x=0;
        TPMC0SC_ELS0x=u8config;
    }
    else if(u8channel==1)
    {
        TPMSC_CPWMS=0;
        TPMC1SC_MS1x=0;
        TPMC1SC_ELS1x=u8config;     
    }
    else
    {
        return;
    }
}



void vfnTPM_ChannelOutputCompare(uint8 u8channel,uint8 u8config)
{
    if(u8channel==0)
    {
        TPMSC_CPWMS=0;
        TPMC0SC_MS0x=1; 
        TPMC0SC_ELS0x=u8config;  
    }
    else if(u8channel==1)
    {
        TPMSC_CPWMS=0;
        TPMC1SC_MS1x = 1;
        TPMC1SC_ELS1x=u8config;
    }
    else
    {
        return;
    }
}

*/
uint16 u16TPM_CheckChannelValue(uint8 u8channel)
{
    if (u8channel == 0) 
    {
        return TPMC0V;   
    }
    else if(u8channel == 1) 
    {
        return TPMC1V;   
    }
    else
    {
        return 0;
    }
}

void vfnTPM_SetChannelValue (uint8 u8channel, uint16 u16value)
{
    if (u8channel == 0) 
    {
        TPMC0V = u16value;   
    }
    else if(u8channel == 1) 
    {
        TPMC1V = u16value;   
    }
    else
    {
        return;
    }
}


